package org.wzys.Controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.wzys.Pojo.PageResult;
import org.wzys.Pojo.ProductQueryparm;
import org.wzys.Pojo.Result;
import org.wzys.Service.CaritemService;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/caritem")
public class CaritemController {

    @Autowired
    private CaritemService caritemService;

    @GetMapping
    public Result getCaritempage(ProductQueryparm parm){
        log.info("获取分页查询的购物车列表");
        PageResult pageResult = caritemService.getCaritempage(parm);
        return Result.success(pageResult);
    }


    //修改逻辑，正在实现
    @PostMapping
    public Result createOrder(@RequestParam List<Integer> productIds,@RequestParam Integer userId) {
        log.info("创建订单，多个商品{}，用户ID{}，对应商品的需求数量",productIds,userId);
        try {
            caritemService.createOrder(productIds,userId);
            return Result.success();
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }
    @PutMapping
    public Result updateOrder(@RequestParam Integer quantity,@RequestParam Integer productId) {
        log.info("修改订单，商品数量{}", quantity);
        caritemService.updateOrder(quantity, productId);
        return Result.success();
    }
    @DeleteMapping
    public Result deleteCaritm(@RequestParam Integer productId) {
        log.info("删除购物车商品{}", productId);
        caritemService.deleteCaritm(productId);
        return Result.success();
    }




}
